package com.example.zuoye.controller;

import com.example.zuoye.entity.Liked;
import com.example.zuoye.resultentity.ResultDT;
import com.example.zuoye.service.GoodLikeService;
import com.example.zuoye.utils.ResultDTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/favour")
public class FavourController {

    @Autowired
    private GoodLikeService goodLikeService;

    /**
     * 关注模块
     * @param liked
     * @return
     */
    @RequestMapping("/isLike")
    public boolean isLike(Liked liked) {
        Liked checked = goodLikeService.checkedLikeService(liked);
        if (checked == null){
            liked.setLikeStatus(1);
            goodLikeService.addLikedService(liked);
            return false;
        }else{
            System.out.println(liked);
            if (checked.getLikeStatus() == 0){
                liked.setLikeStatus(1);
                goodLikeService.updateGoodLike(liked);
                return true;
            }else{
                liked.setLikeStatus(0);
                goodLikeService.updateGoodLike(liked);
                return false;
            }
        }
    }

    /**
     * 检查是否关注
     * @param liked
     * @return
     */
    @RequestMapping("/checkedLikeByUserId")
    public boolean checkedLikeByUserId(Liked liked){
        System.out.println(liked);
        Liked checked = goodLikeService.checkedLikeService(liked);
        if (checked == null){
            liked.setLikeStatus(0);
            goodLikeService.addLikedService(liked);
            return false;
        }else{
            if (checked.getLikeStatus() == 1){
                return true;
            }else{
                return false;
            }
        }
    }

    @RequestMapping("/getListLikeByUserId")
    public ResultDT getListLikeByUserId(@RequestParam("userId") String userId){
        // 查询所有关注信息
        List<Liked> listLikedService = goodLikeService.getListLikedService(userId);
        System.out.println(listLikedService);
        return ResultDTUtils.success(listLikedService);
    }
}
